package com.ssyc.student.activity;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ssyc.common.adapter.VpCommonAdapter;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.utils.ScreenUtils;
import com.ssyc.common.view.NoScrollViewPager;
import com.ssyc.student.R;
import com.ssyc.student.fragment.StudentAchievementGetFragment;
import com.ssyc.student.fragment.StudentAchievementRunningFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生端--成就
 * Created by Administrator on 2018/4/11 0011.
 */

public class StudentAchievementActivity extends BaseActivity implements View.OnClickListener {


    //ViewPager控件
    private NoScrollViewPager vp;

    //适配器
    private VpCommonAdapter adapter;

    //填充的Fragment集合
    private List<Fragment> mFragments;
    private ImageView ivBack;

    //包含Tab页面的线性布局
    private LinearLayout llTab;

    // 进行中的成就
    private TextView tvRunning;

    //已获得的成就
    private TextView tvHasget;
    private LinearLayout llOutside;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_achievement;
    }

    @Override
    protected void init() {
        showContent();

        //初始化视图
        initView();

        //设置ViewPager
        initVp();
    }


    /**
     * 初始化视图的方法
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        llTab = findViewById(R.id.ll_tab);
        tvRunning = findViewById(R.id.tv_running);
        tvRunning.setOnClickListener(this);
        tvHasget = findViewById(R.id.tv_hasget);
        tvHasget.setOnClickListener(this);
        vp = findViewById(R.id.vp);
        llOutside = findViewById(R.id.ll_outside);
        //设置适配
        int screenWidth = ScreenUtils.getScreenWidth(this);
        int width = screenWidth - 16 * 2;
        int height = width * 70 / 470;
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);
        llTab.setLayoutParams(lp);
    }

    /**
     * 设置ViewPager的方法
     */
    private void initVp() {
        mFragments = new ArrayList<>();
        mFragments.add(StudentAchievementRunningFragment.newInstance());
        mFragments.add(StudentAchievementGetFragment.newInstance());
        adapter = new VpCommonAdapter(getSupportFragmentManager(), mFragments);
        vp.setOffscreenPageLimit(mFragments.size());
        vp.setPagerEnabled(false);
        vp.setAdapter(adapter);
    }


    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //关闭页面
            finish();
        } else if (id == R.id.tv_running) {
            //进行中成就
            llOutside.setBackgroundResource(R.drawable.st_chengjiu_running);
            vp.setCurrentItem(0);
        } else if (id == R.id.tv_hasget) {
            //以获得成就
            llOutside.setBackgroundResource(R.drawable.st_chengjiu_hasget);
            vp.setCurrentItem(1);
        }
    }

}
